import { Service , Inject } from "typedi";
import { getRepository,getConnection } from "typeorm";
import {searchValueModel} from '../model/SearchModel';
import {DynamicEntity} from '../entity/DynamicEntity'
import {UserEntity} from '../entity/UserEntity'
import {ThinkEntity} from '../entity/ThinkEntity';
import {SearchrecommendEntity} from '../entity/SearchrecommendEntity'
import { TagEntity } from "../entity/TagEntity";


@Service()
export class SearchDao{

    public async getSearchRecommend(tag_Page:number,tag_Count:number,search_Type:number): Promise<any> {

        return await     getRepository(SearchrecommendEntity)
                            .createQueryBuilder("searchrecommend")
                            .where("searchRecommend_Type = :index", { index: search_Type })
                            .skip(tag_Page*tag_Count)
                            .take(tag_Count)
                            .getMany();
    }


    public async getSearchThinkData(params:searchValueModel): Promise<any> {

        return await   getRepository(ThinkEntity)
        .createQueryBuilder('think')
        .where("think.think_Title LIKE :value", { value: '%'+params.search_Value+'%' })
        .leftJoinAndSelect('think.think_Time','TimeEntity',)
        .leftJoinAndSelect('think.think_Country','CountryEntity',)
        .orderBy("think.think_LikeType", "DESC")
        .skip(params.search_Page*params.search_count)
        .take(params.search_count)
        .getMany()
    }
    public async getSearchUserData(params:searchValueModel): Promise<any> {

        return await  getRepository(UserEntity)
        .createQueryBuilder('UserEntity')
        .where("UserEntity.user_NickName LIKE :value", { value: '%'+params.search_Value+'%' })
        .leftJoinAndSelect('UserEntity.user_UserInfo',"UserInfoEntity")
        .orderBy("UserEntity.user_Fans", "DESC")
        .skip(params.search_Page*params.search_count)
        .take(params.search_count)
        .getMany()
    }
    public async getSearchDynamicData(params:searchValueModel): Promise<any> {


        return await   getRepository(DynamicEntity)
                            .createQueryBuilder("dynamic")
                            .where("dynamic.dynamic_Introduce LIKE :value", { value: '%'+params.search_Value+'%' })
                            .leftJoinAndSelect('dynamic.dynamic_User',"UserEntity")
                            .orderBy("dynamic.dynamic_LikeCount", "DESC")
                            .skip(params.search_Page*params.search_count)
                            .take(params.search_count)
                            .getMany();
    }

    public async getSearchTopicData(params:searchValueModel): Promise<any> {


        return await   getRepository(TagEntity)
                            .createQueryBuilder("tag")
                            .where("tag.tag_Title LIKE :value", { value: '%'+params.search_Value+'%' })
                            .orderBy("tag.tag_SubNum", "DESC")
                            .skip(params.search_Page*params.search_count)
                            .take(params.search_count)
                            .getMany();
    }
}